<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Jobs\SendReminderEmail;
use Auth;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function subscribe()
    {
        ini_set('default_socket_timeout', -1);
        $redis = new \Redis();
        $redis->connect('114.215.222.20', 6445);
        $redis->auth('rxres123456');
        $redis->SUBSCRIBE(array('redisChat'), function ($redis, $chan, $msg) {
            // do something
            echo $msg;
        });
    }
    public function publish()
    {
        $redis = new \Redis();
        $redis->connect('114.215.222.20', 6445);
        $redis->auth('rxres123456');
        var_dump($redis->PUBLISH('test-channel', time()));
        //发送提醒邮件
        //$this->dispatch(new SendReminderEmail());
    }
    /**
     * [index description]
     * @Author   weiyp
     * @DateTime 2017-07-12T18:03:25+0800
     * @return   [type]                   [description]
     */
    public function index(Request $request)
    {
        //Carbon::setTestNow($timezone);
        //
        //
        //
        /*  $client = new \Predis\Client(['host' => '127.0.0.1', 'port' => 6300]);
        $client->auth('rx12345678');
        //$client->set('foo', 'bar');
        $value = $client->get('name');
        for ($i = 0; $i < 100; $i++) {
        echo   $client->get("key:$i").'<br/>';
        }
        $responses = $client->pipeline(function ($pipe) {
        for ($i = 0; $i < 100; $i++) {
        $pipe->set("key:$i", str_pad($i, 4, '0', 0));
        $pipe->get("key:$i");
        }
        });
        $responses = $client->pipeline()->set('foo', 'bar')->get('foo')->execute();
        dd($value);*/
        $key = "example_key";
        $token = array(
            "iss" => "http://example.org",
            "aud" => "http://example.com",
            "iat" => time(),
            "nbf" => 1357000000,
        );

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
        $jwt = JWT::encode($token, $key);
        //print_r($jwt);
        $decoded = JWT::decode($jwt, $key, array('HS256'));

        //print_r($decoded);

/*
NOTE: This will now be an object instead of an associative array. To get
an associative array, you will need to cast it as such:
 */

        $decoded_array = (array) $decoded;

/**
 * You can add a leeway to account for when there is a clock skew times between
 * the signing and verifying servers. It is recommended that this leeway should
 * not be bigger than a few minutes.
 *
 * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
 */
        JWT::$leeway = 60; // $leeway in seconds
        $decoded = JWT::decode($jwt, $key, array('HS256'));
        //dd($decoded);
        return view('admin.index', ['title' => 'admin test']);
        $redis = new \Redis();
/*        $redis->connect('192.168.0.248', 6379);
$redis->auth('E3HngDyKQzyF62tnVzjnJxGUGFhPmkyv');
echo $redis->get("say1").'<br/>';//应输出hello world*/
        $redis->connect('114.215.222.20', 6445);
        $redis->auth('rxres123456');
        $redis->set('Jay13', 'www.jb51.net232323wwww');
        echo 'Jay13:' . $redis->get('Jay13');
        $ss = "";
        /*$redis->LPUSH('runoobkey1','redis1');
        $redis->LPUSH('runoobkey1','mongo1');
        $redis->LPUSH('runoobkey1','mysql1');
        $ss=$redis->LRANGE('runoobkey1',0,10);*/
        $redis->SADD('runoobkey2', 'redis1');
        $redis->SADD('runoobkey2', 'mongo1');
        $redis->SADD('runoobkey2', 'mysql1');
        $ss = $redis->SMEMBERS('runoobkey2');
        echo $redis->ZADD('runoobkey3', 1, 'redis3');
        echo $redis->ZADD('runoobkey3', 2, 'mongo3');
        echo $redis->ZADD('runoobkey3', 3, 'mysql');
        echo $redis->ZADD('runoobkey3', 5, 'mysql');
        $ss = $redis->ZRANGE('runoobkey3', 0, 2, 'WITHSCORES');

        /*$arr = array('h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd');

        foreach ($arr as $k => $v) {

        $redis->rpush("mylist", $v);

        }
        dd($redis->lrange('mylist',0,-1));*/
        $value = $redis->lpop('mylist');

        if ($value) {

            echo "出队的值" . $value;

        } else {

            echo "出队完成";

        }
        dd(34343);
        /*  $redis->connect('127.0.0.1', 6300);
        $redis->auth('rx12345678');
        //$redis->set("say1","hello world");
        echo $redis->get("name").'<br/>';//应输出hello world
        echo "Connection to server sucessfully"; */
        //dd($_SERVER ['HTTP_HOST']);
        $name = '客户你好-发布图片3';
        $imgPath = 'http://laravelacademy.org/wp-statics/images/carousel/LaravelAcademy.jpg';
        $imgPath = 'http://' . $_SERVER['HTTP_HOST'] . '/app/doc/1.jpg';
        //$image = Storage::get('doc/1.jpg');
        $flag = \Mail::send('admin.test', ['name' => $name, 'imgPath' => $imgPath], function ($message) {
            $to = '1020472637@qq.com';
            $message->to($to)->subject('测试邮件');
            $attachment = storage_path('app/doc/2017.docx');

            //在邮件中上传附件
            $message->attach($attachment, ['as' => "=?UTF-8?B?" . base64_encode('测试文档') . "?=.docx"]);
        });
        if ($flag) {
            echo '发送邮件成功，请查收！';
            //return view('admin.test', ['name' => $name, 'imgPath' => $imgPath]);
        } else {
            echo '发送邮件失败，请重试！';
        }
        /*
        echo Carbon::now()->tzName . '<br/>';
        echo Carbon::now()->toDateTimeString() . '<br/>';
        echo Carbon::parse('2 days ago')->toDateString() . '<br/>';
        echo Carbon::parse('+3 days')->toDateString() . '<br/>';
        echo Carbon::parse('+1 weeks')->toDateString() . '<br/>';
        echo Carbon::parse('+2 months')->toDateString() . '<br/>';

        // Carbon::setTimezone($timezone);
        $timespan = strtotime('2017-07-21 10:00:00');
        echo $timespan . '<br/>';
        echo date('Y-m-d H:i:s', $timespan) . '<br/>';
        $dt = Carbon::createFromTimestamp(strtotime('2017-07-20 13:45:00'));
        $ss = '2017-07-21 14:14:58';
        $de = Carbon::createFromTimestamp(strtotime($ss));
        echo Carbon::now() . '<br/>';
        $days = $dt->diffForHumans($de);
        echo $days . '<br/>';
        echo Carbon::now()->subDays(5)->diffForHumans() . '<br/>';
        echo Carbon::now()->subDays(24)->diffForHumans() . '<br/>'; // 3 weeks ago
        echo Carbon::now()->subDays(24)->diffForHumans(null, true) . '<br/>';
        //dd($dt);
        function closureFunc1()
        {
        $func = function () {
        echo "hello";
        };
        $func();
        }
        closureFunc1();*/
/*        function closureFunc3()
{
$num = 1;
$func = function () use (&$num) {
echo $num;
$num++;
};
return $func;
}
$func = closureFunc3(); //函数返回匿名函数
$func(); //然后我们在用$func() 调用
$func();
closureFunc3()();*/
        /* function closureFunc5()
    {
    $num = 1;
    $func = function () use (&$num) {
    echo "<br/>";
    $num++;
    echo $num;
    };
    echo "<br/>";
    echo $num;
    return $func;
    }
    $func = closureFunc5();
    $func();
    $func();
    $func();

    return view('admin.index', ['title' => 'admin test']);*/
    }
    public function test()
    {
        return view('admin.test');
    }
    public function search()
    {
        $user = Auth::user();
        /* $user=Auth::user();
        var_dump($user->hasRole('admin')); //
        var_dump($user->may('create')); // true

        var_dump($user->ability('admin', 'create'));
        var_dump(Entrust::ability('admin', 'create'));
        dd($user);*/
        /*var_dump($user->ability('admin', 'create,edit'));
        var_dump(Entrust::ability('admin', 'create,search'));*/
        $user->hasRole('admin');
        if ($user->may('search')) {
            dd('search');
        } else {
            abort(404);
        }

    }
    public function create()
    {
        $user = Auth::user();
        $user->hasRole('admin');
        if ($user->may('create')) {
            dd('create');
        } else {
            abort(404);
        }

    }
    public function edit()
    {
        $user = Auth::user();
        $user->hasRole('admin');
        if ($user->may('edit')) {
            dd('edit');
        } else {
            abort(404);
        }
    }
    public function detail()
    {

        $user = Auth::user();
        $user->hasRole('admin');
        if ($user->may('detail')) {
            dd('detail');
        } else {
            abort(404);
        }
    }
    public function echart()
    {
        return view('admin.echart.index');
    }
}
